package web.dmt.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import web.dmt.dao.CorporationDAO;
import web.dmt.dao.factory.DAOFactory;
import web.dmt.vo.Corporation;



public class CorporationDAOImpl implements CorporationDAO {
private Connection conn=null;
private PreparedStatement pstmt=null;
public CorporationDAOImpl(Connection conn){
	this.conn=conn;
}
	public boolean doCreate(Corporation corporation) throws Exception {
		boolean flag=false;
		String sql="insert into corporation(cwhere,cname,crole,cplay,csystem,content,chost,cteacher,atype,) values(?,?,?,?,?,?,?,?,?)";
		this.pstmt=(PreparedStatement) this.conn.prepareStatement(sql);
		this.pstmt.setString(1,corporation.getCwhere());
		this.pstmt.setString(2,corporation.getCname());
		this.pstmt.setString(3,corporation.getCrole());
		this.pstmt.setString(4,corporation.getCplay());
		this.pstmt.setString(5,corporation.getCsystem());
		this.pstmt.setString(6,corporation.getContent());
		this.pstmt.setInt(2,corporation.getChost().getUid());
		this.pstmt.setString(7,corporation.getCteacher());
		this.pstmt.setInt(1,corporation.getAtype().getFid());//.getFid()
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		return flag;
	}
	public Corporation findById(Integer cid) throws Exception {
		Corporation corporation=null;
		String sql="select cid,cwhere,cname,crole,cplay,csystem,content,cphono,chost,cteacher,clabel,atype from corporation where cid=?";
		this.pstmt=(PreparedStatement) this.conn.prepareStatement(sql);
		this.pstmt.setInt(1,cid);
		ResultSet rs=this.pstmt.executeQuery();
		while(rs.next()){
			corporation=new Corporation();
			corporation.setCid(rs.getInt(1));
			corporation.setCname(rs.getString(2));
			corporation.setCwhere(rs.getString(3));
			corporation.setCname(rs.getString(4));
			corporation.setCrole(rs.getString(5));
			corporation.setCplay(rs.getString(6));
			corporation.setCsystem(rs.getString(7));
			corporation.setContent(rs.getString(8));
			corporation.setCphono(rs.getString(9));
			corporation.setChost(DAOFactory.getMyUserDAOInstance().findById(rs.getInt(10)));
			corporation.setCteacher(rs.getString(11));
			corporation.setClabel(rs.getString(12));
			corporation.setAtype(DAOFactory.getFiledDAOInstance().findById(rs.getInt(13)));//DAOFactory.getCorporationDAOInstance().findById(rs.getInt(2))
		}
		return corporation;
	}
	public boolean doUpdate(Corporation corporation)throws Exception{
		boolean flag=false;
		String sql="update corporation set cteachet=?,clabel=?,content=? where cid=?";
		this.pstmt=(PreparedStatement)this.conn.prepareStatement(sql);
		this.pstmt.setString(1,corporation.getCteacher());
		this.pstmt.setString(2,corporation.getClabel());
		this.pstmt.setString(3,corporation.getContent());
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		return flag;
	}
}